package com.kgo.eidtor.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.google.gson.Gson;
import com.kgo.eidtor.dao.InterfaceBeanMapper;
import com.kgo.eidtor.entity.BackupBean;
import com.kgo.eidtor.entity.InterfaceBean;
import com.kgo.eidtor.service.BackupService;
import com.kgo.eidtor.service.InterfaceService;
import com.kgo.eidtor.util.ResultUtil;
import com.kgo.eidtor.websocket.WebSocketServer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

@Service
public class InterfaceServiceImpl implements InterfaceService {
    @Resource
    private InterfaceBeanMapper interfaceBeanMapper;
    @Resource
    private BackupService backupService;

    private Gson _gs = new Gson();
    @Override
    public String addInterface(InterfaceBean interfaceBean) {
        if (StringUtils.isBlank(interfaceBean.getId())){
            interfaceBean.setId(IdUtil.simpleUUID());
        }
        int count =  interfaceBeanMapper.insert(interfaceBean);
        if (count>0){
            return ResultUtil.add(count,"新增接口成功！",interfaceBean.getId());
        }else {
            return ResultUtil.add(count,"新增接口失败！");
        }

    }

    @Override
    public String getInterfaces(InterfaceBean interfaceBean) {
        int count = interfaceBeanMapper.getInterfaceCount(interfaceBean);
        if (count>0){
            return ResultUtil.query(count,interfaceBeanMapper.getInterfaces(interfaceBean),"查询接口成功！");
        }else{
            return ResultUtil.query(count,null,"查询接口失败 ！");
        }
    }

    @Override
    public int getInterfaceCount(InterfaceBean interfaceBean) {
        return interfaceBeanMapper.getInterfaceCount(interfaceBean);
    }

    @Override
    public String getInterfaceById(String id) {
        InterfaceBean interfaceInfo = interfaceBeanMapper.selectByPrimaryKey(id);
        if (ObjectUtil.isNull(interfaceInfo)){
           return ResultUtil.query(0,null,"查询接口信息失败");
        }else {
            return ResultUtil.query(1,interfaceInfo,"查询接口信息成功");
        }
    }

    @Override
    public String updateInterfaceById(InterfaceBean interfaceBean) {
        if (StringUtils.isBlank(interfaceBean.getId())){
           return ResultUtil.update(-1,"接口ID为空，更新失败！");
        }
        /**
         *  更新接口之前需要备份之前的数据
         */
        //1：查询一下该接口的所有信息
        InterfaceBean interfaceInfo = interfaceBeanMapper.selectByPrimaryKey(interfaceBean.getId());
        if(ObjectUtil.isNull(interfaceInfo)){
            return ResultUtil.update(-1,"更新失败，数据库无该接口信息！");
        }
        //2: 声明备注对象开始备份数据
        BackupBean backupInterface =  new BackupBean();
        backupInterface.setId(IdUtil.simpleUUID());
        backupInterface.setBackupsRemark("备份接口数据！");
        backupInterface.setContentId(interfaceBean.getId());
        backupInterface.setSort("interface");
        backupInterface.setName("接口数据备份");
        //版本号
        if (StringUtils.isNotBlank(interfaceInfo.getVersion())){
            backupInterface.setBackupsVersion(interfaceInfo.getVersion());
            //如果版本号相同，就返回
            if(interfaceInfo.getVersion().equals(interfaceBean.getVersion())){
                return ResultUtil.update(-1,"更新失败，未更新版本号，请输入新的版本号！");
            }
        }
        if(StringUtils.isNotBlank(interfaceInfo.getProjectId())){
            backupInterface.setProjectId(interfaceInfo.getProjectId());
        }
        backupInterface.setBackupsText(_gs.toJson(interfaceInfo));

        int backIsSuccess = backupService.addBackup(backupInterface);
        if (backIsSuccess<=0){
            return ResultUtil.update(-1,"更新失败，接口备份失败！请联系系统管理员");
        }

        int count = interfaceBeanMapper.updateByPrimaryKey(interfaceBean);
        if (count>0){
            try {
                WebSocketServer.sendToAll("接口更新");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return ResultUtil.update(count,"接口更新成功！");
        }else {
            return ResultUtil.update(count,"接口更新失败！");
        }
    }

}
